<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>商城后台</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.5.5/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/lib/zTree/css/zTreeStyle/zTreeStyle.css}" >
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <input type="hidden" name="id" th:value="${model.id}" />
    <input type="hidden" id="selectType" th:value="${model.type}" />
    <input type="hidden" id="selectBigAreaCode" th:value="${model.bigAreaCode}" />

    <div class="layui-form-item" >
        <label class="layui-form-label required">国家</label>
        <div class="layui-input-block" >
            <select id="countryCode"  name="countryCode"  lay-search="" >
                <option value="CHN">中国</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item" >
        <label class="layui-form-label required">大区</label>
        <div class="layui-input-block" >
            <select  id="bigAreaCode"  name="bigAreaCode" lay-search="" >
                <option value="HUADONG" selected>华东</option>
                <option value="HUABEI" >华北</option>
                <option value="HUAZHONG" >华中</option>
                <option value="HUANAN" >华南</option>
                <option value="DONGBEI" >东北</option>
                <option value="XIBEI" >西北</option>
                <option value="XINAN" >西南</option>
                <option value="GANGAOTAI" >港澳台</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item" >
        <label class="layui-form-label required">类型</label>
        <div class="layui-input-block" th:if="${model.type eq 1}" >
            <input type="radio" name="type" lay-filter="type" value="1" title="省" checked="">
            <input type="radio" name="type" lay-filter="type" value="2" title="市">
            <input type="radio" name="type" lay-filter="type" value="3" title="区县" >
        </div>
        <div class="layui-input-block" th:if="${model.type eq 2}" >
            <input type="radio" name="type" lay-filter="type" value="1" title="省" >
            <input type="radio" name="type" lay-filter="type" value="2" title="市" checked="">
            <input type="radio" name="type" lay-filter="type" value="3" title="区县" >
        </div>
        <div class="layui-input-block" th:if="${model.type eq 3}" >
            <input type="radio" name="type" lay-filter="type" value="1" title="省" >
            <input type="radio" name="type" lay-filter="type" value="2" title="市">
            <input type="radio" name="type" lay-filter="type" value="3" title="区县" checked="">
        </div>
    </div>
    <div class="layui-form-item" id="provinceDiv">
        <label class="layui-form-label required" >省</label>
        <div class="layui-input-block">
            <input type="text" name="province" id="province" th:value="${model.province}"  maxlength="20" lay-reqtext="省不能为空" placeholder="请输入省名称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" id="cityDiv" >
        <label class="layui-form-label required" >市</label>
        <div class="layui-input-block">
            <input type="text" name="city" id="city"  th:value="${model.city}" maxlength="20" lay-reqtext="市不能为空" placeholder="请输入市名称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" id="areaDiv">
        <label class="layui-form-label required" >区县</label>
        <div class="layui-input-block">
            <input type="text" name="area" id="area" th:value="${model.area}"  maxlength="20" lay-reqtext="区县不能为空" placeholder="请输入区县名称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required ">编码</label>
        <div class="layui-input-block">
            <input type="text" name="code"   lay-verify="required" th:value="${model.code}"   maxlength="20" lay-reqtext="编码不能为空" placeholder="请输入编码" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">上级节点</label>
        <div class="layui-input-block">
            <input type="hidden" name="parentCode" id="parentCode" th:value="${model.parentCode}" >
            <input type="hidden" name="pid" id="pid" th:value="${model.pid}" >
            <input type="hidden" name="appCode" id="appCode" th:value="${model.appCode}" >
            <input type="text" readonly  id="selectPid" lay-verify="required"  placeholder="请选择上级节点" th:value="${model.parentName}"  value="" class="layui-input" >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">直辖市</label>
        <div class="layui-input-block" th:if="${model.isMunicipality eq 0}" >
            <input type="radio" name="isMunicipality" value="0" title="否" checked="">
            <input type="radio" name="isMunicipality" value="1" title="是">
        </div>
        <div class="layui-input-block" th:if="${model.isMunicipality eq 1}" >
            <input type="radio" name="isMunicipality" value="0" title="否">
            <input type="radio" name="isMunicipality" value="1" title="是"  checked="">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">排序</label>
        <div class="layui-input-block">
            <input type="number" name="areaSort"    placeholder="请输入排序"  th:value="${model.areaSort}"  value="" class="layui-input" lay-verify="number">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注信息</label>
        <div class="layui-input-block">
            <textarea name="remark" class="layui-textarea" maxlength="255" placeholder="请输入备注信息" th:text="${model.remark}" ></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script th:src="@{/lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script>
    layui.use(['form','layer','tree'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        var index;
        var tree = layui.tree;
        var loadding;
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            if(data.field.type=="1")
            {
                if(data.field.province==null||data.field.province=="") {
                    layer.msg("请输入省份名称");
                    return false;
                }
            }else if(data.field.type=="2")
            {
                if(data.field.city==null||data.field.city=="") {
                    layer.msg("请输入市名称");
                    return false;
                }
            }else if(data.field.type=="3")
            {
                if(data.field.area==null||data.field.area=="") {
                    layer.msg("请输入区县名称");
                    return false;
                }
            }
            $.ajax({
                url:"[[@{/area/update}]]",
                contentType: "application/json; charset=utf-8",
                type:'POST',
                data:JSON.stringify(data.field),
                success:function(data){
                    if(data.code==0) {
                        layer.msg(data.msg);
                        return false;
                    }
                    var index = layer.alert(data.msg, {
                        title: '提示信息'
                    }, function () {
                        // 关闭弹出层
                        layer.close(index);
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.location.reload();
                        parent.layer.close(iframeIndex);

                    });
                }
            });
            return false;
        });


        //初始化默认
        var selectType = $("#selectType").val();
        if(selectType=="1") {
            $("#provinceDiv").show();
            $("#cityDiv").hide();
            $("#areaDiv").hide();

        }else if(selectType=="2") {
            $("#provinceDiv").hide();
            $("#cityDiv").show();
            $("#areaDiv").hide();

        }else if(selectType=="3") {
            $("#provinceDiv").hide();
            $("#cityDiv").hide();
            $("#areaDiv").show();

        }

        var selectBigAreaCode = $("#selectBigAreaCode").val();
        $("#bigAreaCode").find("option:contains('"+selectBigAreaCode+"')").attr("selected",true);

        form.on('radio(type)', function (data) {
            var typeValue = data.value;
            if(typeValue=="1") {
                $("#provinceDiv").show();
                $("#cityDiv").hide();
                $("#areaDiv").hide();

                $("#city").val("");
                $("#area").val("");

            }else if(typeValue=="2") {
                $("#provinceDiv").hide();
                $("#cityDiv").show();
                $("#areaDiv").hide();

                $("#province").val("");
                $("#area").val("");

            }else if(typeValue=="3") {
                $("#provinceDiv").hide();
                $("#cityDiv").hide();
                $("#areaDiv").show();

                $("#province").val("");
                $("#city").val("");
            }
        });



        function filter(treeId, parentNode, data) {
            return data.data;
        }

        $("#selectPid").click(function(){
            index = layer.open({
                title: '选择上级节点',
                type: 1,
                shade: 0.2,
                maxmin:true,
                area: ['20%', '60%'],
                btn: ['确定'],
                content: "<ul id=\"areaTree\" style='height:90%' class=\"ztree\"></ul>",
                success:function(layero){

                    loadding = layer.load();

                    var setting = {
                        async: {
                            enable: true,
                            url:"[[@{/}]]area/query/tree/child",
                            autoParam:["id", "pid", "name"],
                            dataFilter: filter
                        },
                        data:{
                            simpleData:{
                                enable: true,
                                idKey:'id',
                                pIdKey:'pid',
                                rootPId: -1
                            }
                        },
                        view:{
                            showIcon: true
                        }
                    };

                    $.ajax({
                        url:"[[@{/}]]area/query/tree/child",
                        type:"POST",
                        dataType: "json",
                        data: {"id": null},
                        success: function(data){
                            zTreeObj = $.fn.zTree.init($("#areaTree"),setting, data.data);
                        },
                        error: function(){

                        },
                        complete:function()
                        {
                            layer.close(loadding);
                        }
                    });



                },
                yes: function(index, layero){
                    var checkedNodes = zTreeObj.getSelectedNodes();
                    if(checkedNodes==null||checkedNodes.length<=0)
                    {
                        layer.msg("请选择节点");
                        return false;
                    }
                    var selectNode = checkedNodes[0];
                    $("#selectPid").val(selectNode.name);
                    $("#parentCode").val(selectNode.code);
                    $("#pid").val(selectNode.id);
                    $("#appCode").val(selectNode.appCode);
                    layer.close(index);
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });

        })
    });
</script>



<script type="text/javascript" th:src="@{/lib/zTree/js/jquery.ztree.core.js}"></script>
<script type="text/javascript" th:src="@{/lib/zTree/js/jquery.ztree.excheck.js}"></script>
<script type="text/javascript" th:src="@{/lib/zTree/js/jquery.ztree.exedit.js}"></script>

</body>
</html>